package service.proxy;

import com.itany.jdbc.jdbc.TransactionManager;
import execption.ServiceExecpion;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author 陈华旺
 * @version 1.0
 * @date 2023年10月27日 10:30
 * @Description TODO
 */
public class ProxyTransaction implements InvocationHandler {
    private final Object target;
    public ProxyTransaction(Object target) {
        this.target = target;
    }
    private TransactionManager tm = new TransactionManager();
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {
        Object o = null;
        try {
            tm.begin();
            o = method.invoke(this.target,args);
            tm.commit();
        } catch (Exception e) {
            tm.rollback();
            throw new ServiceExecpion("服务器忙:",e);
        }
        return o;
    }
}
